<?php				
	interface asset_class_info
	{		
		function getAssetInfo($assCode);
		function getQuantity($catCode);
		function getAssetInfo_catCode($catCode);
		function getQuantity_Xuat($catCode);
		function getQuantity_Nhap($catCode);
		function getAssetNameList();
		function getAssetNameList_catCode($catCode);
		function getQuantity_Nhap_Date($catCode,$assName,$fromDate,$toDate);
		function getQuantity_Xuat_Date($catCode,$assName,$fromDate,$toDate);
		function getQuantityXPB($catCode,$assName,$fromDate,$toDate);
		//Lấy giá hàng hóa không barcode
		function getAssetNoBarcodePrice($catCode,$assName);
	}
	
	class asset_class implements asset_class_info
	{
		function asset_class()
		{
			@session_start(); 
			require_once('../../lib/'.'BaseDir.inc.htm');
			require_once(EIU_LIB.'DBCommon.class.htm');					
		}
			
		function getAssetInfo($assCode)
		{

			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q = "SELECT a.`AssCode`, a.`AssName`,a.`STAID`,a.`Unit`,s.`DescriptionSta`,r.`RoomCode`,b.`BlockCode`,a.`AssDescription` FROM `asset_tbl` as a,`status_tbl` as s,`room_tbl` as r,`block_tbl` as b WHERE a.`isDeleted`=0 and r.`isDeleted`=0 and b.`isDeleted`=0 and s.`isDeleted`=0 and a.`STAID`=s.`STAID` and a.`LocaID`=r.`RoomID` and r.`BlockID`=b.`BlockID` and a.`AssCode`='".$assCode."'";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;			
			
		}
		//tong so luong nhap cua thiet bi khong dung barcode
		function getQuantity($catCode)	
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			/*$q ="SELECT sum(`Quantity`)- 
                     (SELECT sum(d1.`Quantifty`)-(SELECT sum(d2.`Quantifty`) FROM `detail_io_tbl` as d2 WHERE d2.`isDeleted`=0 and d2.`CATCode`='".$catCode."' and d2.`IOCode` Like '%N%')
                      FROM `detail_io_tbl` as d1
                      WHERE d1.`isDeleted`=0 and d1.`CATCode`='".$catCode."' and d1.`IOCode` Like '%X%' ) as SL 
				FROM `asset_tbl` 
				WHERE `isDeleted`=0 and `CATCode`='".$catCode."'";*/
			//echo $q;
			$q="SELECT sum(`Quantity`) as SL FROM `asset_tbl` WHERE `isDeleted`=0 and `CATCode`='".$catCode."'";
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;	
		}
		
		//tong so luong xuat cua thiet bi khong dung barcode
		function getQuantity_Xuat($catCode)	
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();								
			
			$q="SELECT sum(`Quantifty`) as SLX FROM `detail_io_tbl` WHERE `isDeleted`=0 and `CATCode`='".$catCode."' and `IOCode` Like '%X%'";
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;	
		}
		
		/*--------------------------------------------------------------------------
		@author: 	Hien Huynh
		@des:		Tong so luong nhap cua thiet bi khong dung barcode trong asset_tbl
		@date:		09/09/2014
		@param: 	$assetID -> AssetName; $localID: RoomID cua table room_tbl				
		@return:	AssetName, SumQuanlity
		--------------------------------------------------------------------------*/
		function getSumQuantityOfAsset($assName,$localID,$catCode)	
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();								
			
			$q="SELECT `Unit`,SUM(`Quantity`) AS Total FROM asset_tbl WHERE `AssName`='$assName' AND `CATCode`='$catCode' AND `LocaID`='$localID' AND `isDeleted`=0";
		//	print_r($q);exit();
			$rs = $oDB->stl_db_select($q, $conn);
		//	print_r($rs);
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;	
		}
		
		/*--------------------------------------------------------------------------
		@author: 	Hien Huynh
		@des:		Tong so luong nhap/xuat noi bo cua thiet bi khong dung barcode
		@date:		15/09/2014
		@param: 	$assName, $localID, $catCode, $type (N or X)				
		@return:	AssetName, SumQuanlity
		--------------------------------------------------------------------------*/
		function getSumQuantityOfDetailIO($assName, $localID, $catCode, $type)	
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();								
			
			$q="SELECT SUM(d.`Quantifty`) as Total 
				FROM `detail_io_tbl` as d LEFT JOIN `io_tbl` as i ON (d.`IOCode`=i.`CostCode`) 
				WHERE LEFT(d.`IOCode`,1)='$type' AND d.`AssName`='$assName' AND d.`CATCode`='$catCode' AND d.`isDeleted`=0";
		//	print_r($q);
			$rs = $oDB->stl_db_select($q, $conn);
		//	print_r($rs);
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs[0]->Total;
				else return false;		
		}		
		
		//tong so luong nhap lai thiet bi tu cac phong ban khong dung barcode
		function getQuantity_Nhap($catCode)	
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();								
			
			$q="SELECT sum(`Quantifty`) as SLN FROM `detail_io_tbl` WHERE `isDeleted`=0 and `CATCode`='".$catCode."' and `IOCode` Like '%N%'";
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;	
		}
		
		
		function getAssetInfo_catCode($catCode)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			$q ="SELECT DISTINCT a.`STAID` ,a.`Unit`, s.`DescriptionSta` , r.`RoomCode` , b.`BlockCode` , a.`AssDescription` 
				FROM  `asset_tbl` AS a,  `status_tbl` AS s,  `room_tbl` AS r,  `block_tbl` AS b
				WHERE a.`isDeleted` =0 AND r.`isDeleted` =0 AND b.`isDeleted` =0 AND s.`isDeleted` =0 AND a.`STAID` = s.`STAID` AND a.`LocaID` = r.`RoomID` AND r.`BlockID` = b.`BlockID` AND a.`CATCode`='".$catCode."'";
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;		
		}
		function getAssetNameList()
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$query = "SELECT distinct(`AssName`) FROM `asset_tbl` WHERE `isDeleted`=0";			
			
			$res = $oDB->stl_db_select($query, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($res)	return $res;
			else return false;
			
		}
		//lay danh sach thiet bi theo catCode
		function getAssetNameList_catCode($catCode)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$condition = " WHERE `isDeleted` = 0";
			if($catCode!="") $condition.=" AND `CATCode`='".$catCode."'";			
			
			$query = "SELECT distinct(`AssName`) FROM `asset_tbl`".$condition;			
			
			$res = $oDB->stl_db_select($query, $conn);
			
			$oDB->stl_closeConn($conn);
			
			if($res)	return $res;
			else return false;
			
		}
		//lay so luong thiet bi nhap theo catCode, tu ngay den ngay
		function getQuantity_Nhap_Date($catCode,$assName,$fromDate,$toDate)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
									
			/*$q ="SELECT distinct(`AssName`),sum(`Quantity`) as SLN FROM `asset_tbl` 
				WHERE `CATCode`='".$catCode."' and `AssName` LIKE '%".$assName."' and `isDeleted`=0 and Date(`CreateDate`) between '".$fromDate."' and '".$toDate."' 
				Group by `AssName`";*/
			//echo $q;
			
			$condition = " WHERE `isDeleted` = 0";
			if($catCode!="") $condition.=" AND `CATCode`='".$catCode."'";
			if($assName) $condition.="AND `AssName` LIKE '%".$assName."'";
			if(($fromDate!="")&&($toDate!="")) $condition.="AND Date(`CreateDate`) between '".$fromDate."' and '".$toDate."'";
			if(($fromDate!="")&&($toDate=="")) $condition.="AND Date(`CreateDate`) between '".$fromDate."' and now()";
			if(($fromDate=="")&&($toDate!="")) $condition.="AND Date(`CreateDate`) between '' and '".$toDate."'";
			
			$q="SELECT distinct(`AssName`),sum(`Quantity`) as SLN FROM `asset_tbl`".$condition."Group by `AssName`";
			$rs = $oDB->stl_db_select($q, $conn);
			 
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;	
		}
		function getQuantity_Xuat_Date($catCode,$assName,$fromDate,$toDate)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			/*$q="SELECT d.`AssName`,sum(d.`Quantifty`) as SLX 
			FROM `detail_io_tbl` as d,`io_tbl` as i  
			WHERE d.`isDeleted`=0 and LEFT(d.`IOCode`,1)='X' and d.`AssName`LIKE '%".$assName."' and d.`CATCode`='".$catCode."' and d.`IOCode`=i.`CostCode` and DATE(i.`DeliveryReceiptDate`) between '".$fromDate."' and '".$toDate."'
			Group by d.`AssName`";	*/
			
			$condition ="";
			if($assName!="") $condition.="AND d.`AssName`LIKE '%".$assName."'";
			if($toDate!="") $condition.="and DATE(i.`DeliveryReceiptDate`) between '".$fromDate."' and '".$toDate."'";
			$q="SELECT d.`AssName`,sum(d.`Quantifty`) as SLX 
			FROM `detail_io_tbl` as d,`io_tbl` as i  
			WHERE d.`isDeleted`=0 and LEFT(d.`IOCode`,1)='X' and d.`CATCode`='".$catCode."' and d.`IOCode`=i.`CostCode`".$condition."Group by d.`AssName`";
			
			$rs = $oDB->stl_db_select($q, $conn);
			 
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;	
		}
		function getQuantityXPB($catCode,$assName,$fromDate,$toDate)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
			
			$condition ="";
			if($catCode!="") $condition.="and d.`CATCode`='".$catCode."'";
			if($assName!="") $condition.="AND d.`AssName`LIKE '%".$assName."'";
			if(($fromDate!="")&&($toDate!="")) $condition.="AND DATE(i.`DeliveryReceiptDate`) between '".$fromDate."' and '".$toDate."'";
			
			
			$q="SELECT d.`AssName`,d.`Quantifty`,i.`DeliveryReceiptDate`,i.`RoomID_Out`,i.`Note`, r.`RoomCode`,b.`BlockCode`,r.`RoomName` 
				FROM `detail_io_tbl` as d, `io_tbl` as i,`room_tbl`as r, `block_tbl` as b
				WHERE i.`CostCode`=d.`IOCode` and LEFT(d.`IOCode`,1)='X' 
				and r.`RoomID`=i.`RoomID_Input` and b.`BlockID`=r.`BlockID`".$condition.
				"Order by  d.`AssName`";		
			
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			 
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;
		}
		
		function getAssetNoBarcodePrice($catCode,$assName)
		{
			$oDB = new DBCommon();
			$conn =  $oDB->stl_openConn();
						
			$q = "
				SELECT `ID`,`CATCode`,`AssName`,`Price`,`Quantity`,`Unit` 
				FROM `price_tbl` 
				WHERE `isDeleted` = 0 AND `CATCode` = '".$catCode."' AND `AssName` = '".$assName."' 
				ORDER BY `ID` DESC 
				LIMIT 0,1		
			";
			
			//echo $q;
			$rs = $oDB->stl_db_select($q, $conn);
			 
			$oDB->stl_closeConn($conn);
			
			if($rs)	return $rs;
			else return false;
		}
	}
?>